const Biz = require('../biz/index');
const utils = require('../core/utils');

module.exports = {
    init(router,urlencodedParser,checkLogin,setLoginToken,loginUsers){
        // todo 应该是商户登录 还是用户登录？
        router.post('/merchant/login', urlencodedParser, function (req, res) {
            let param = typeof req.body == 'string' ? JSON.parse(req.body) : req.body;
            let cData = {
                mobile:param.account,
                password:param.password,
            };
            Biz.User.login(cData,function(data){
                let loginRes = JSON.parse(JSON.stringify(data));
                let token = utils.getGuid();
                loginRes.data['token'] = token;
                if(setLoginToken)setLoginToken(token,{
                    userId:loginRes.data.id,
                    merchantId:loginRes.data.merchantId
                });
                delete loginRes.data.password;
                // loginUsers[token] = data.data.id; // 记录用户id和token一对一。
                res.send(loginRes);
            },function(data){
                res.send(data);
            });
        });
        router.post('/user/create', urlencodedParser, function(req, res) {
            let param = utils.parseBody(req);
            let token = req.get('token');
            let cData = {
                username:param.username,
                mobile:param.mobile,
                password:param.password,
                role:param.role || 2,
                wxcode: param.wxcode || null,
                head_image:param.head_image || null,
                merchant_id:loginUsers[token]['merchantId']
            };
            Biz.User.create(cData,(data)=>{
                res.send(data);
            },(data)=>{
                res.send(data);
            });
        });
        router.post('/user/update', urlencodedParser, function(req, res) {
            checkLogin(req,res,function(){
                let userData = utils.parseBody(req);
                Biz.User.update(userData,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            });
        });
        router.post('/user/delete', urlencodedParser, function(req, res) {
            checkLogin(req,res,function(){
                console.log('执行删除账户业务');
                let userData = utils.parseBody(req);
                Biz.User.delete(userData,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            });
        });
        router.post('/user/search', urlencodedParser, function(req, res) {
            checkLogin(req,res,function(){
                let param = utils.parseBody(req);
                let token = req.get('token');
                param.merchant_id = loginUsers[token]['merchantId'];
                Biz.User.search(param,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            });
        });
    }
};
